<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="x-ua-compatible" content="ie=edge">
		<title></title>
		<meta name="description" content="Neat">
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" user-scalable="no">
		<link rel="stylesheet" href="index.css">
		<script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
		<script src="https://www.jq22.com/demo/sweetalert-master-150108224250/lib/sweet-alert.js"></script>
		<link rel="stylesheet" href="https://www.jq22.com/demo/sweetalert-master-150108224250/lib/sweet-alert.css">
	</head>
	<body>

		<div class="main-group">
			<h1 style="font-size: 28px;text-align: center;">报平安登记</h1>
			<div class="form-group">
				<div class="form" id="form">

					<select name="" id="classNum" class="text-input">
						<option value="00">选择班级</option>
					</select>
					<input class="text-input" placeholder="输入QQ" id="qq">
					<button type="submit" class="btn submit-btn" onclick="set()">绑定</button>
				</div>
			</div>
			<div id="note">
				<div align="center"><span id="name"></span>的记录</div>
				<ul class="items-group">

				</ul>
			</div>
			<div class="icon success animate" style="display: block;"> <span class="line tip animateSuccessTip"></span>
				<span class="line long animateSuccessLong"></span>
				<div class="placeholder"></div>
				<div class="fix"></div>
			</div>
		</div>


	</body>
</html>
<script type="text/javascript">
	$(document).ready(function() {

		$.get(`https://39.107.221.29/studentBpa/classNum.php`,
			function(data, status) {
				data = JSON.parse(data)
				for (let i of data.number.split(",")) {
					$("#classNum")[0].innerHTML += `<option value="${i}">22${i<10?"0"+i:i}</option>`
				}
			})

		$("#note").hide()
		if (localStorage.getItem("bpaName") && localStorage.getItem("bpaClassNum") && localStorage.getItem(
				"bpaQQ")) {
			$("#form").hide()
			$("#note").show()
			$("#name").html(localStorage.getItem("bpaName"))
			let bpaName = localStorage.getItem("bpaName")
			let bpaClassNum = localStorage.getItem("bpaClassNum")
			let bpaNum = localStorage.getItem("bpaNum")
			let month = new Date().getMonth() + 1
			let day = new Date().getDate()

			$.get(`https://39.107.221.29/studentBpa/getAll.php?name=${bpaName}`,
				function(data, status) {
					data = JSON.parse(data)
					for (let i of data) {
						$(".items-group")[0].innerHTML += `
						<li class="item" style="left: 0px; opacity: 1;">
						<span class="text">${i.month}月${i.day}日</span>
						<span class="icon">√</span></li>
`
					}
				})

			$.get(`https://39.107.221.29/studentBpa/add.php?classNum=${bpaClassNum}&name=${bpaName}&num=${bpaNum}&month=${month}&day=${day}`,
				function(
					data, status) {
					swal({
						title: "报平安提交",
						type: JSON.parse(data).code == 200 ? "success" : "error",
						text: JSON.parse(data).msg,
						confirmButtonText: "完成"
					});
				})

		}
	})

	function set() {
		let classNum = $("#classNum").val()
		let qq = $("#qq").val()
		$.get(`https://39.107.221.29/studentBpa/getStu.php?classNum=${classNum}&qq=${qq}`, function(data, status) {
			if (data == "") {
				swal({
					title: " 绑定失败",
					type: "error",
					text: "绑定失败，请检查班级和QQ是否正确。如是20号及以后进群的同学请联系代班添加信息",
					confirmButtonText: "好的"
				});
			} else {
				data = JSON.parse(data)
				if (data.name) {
					localStorage.setItem("bpaName", data.name)
					localStorage.setItem("bpaNum", data.num)
					localStorage.setItem("bpaClassNum", classNum)
					localStorage.setItem("bpaQQ", qq)
				}
				swal({
					title: " 绑定成功",
					type: "success",
					text: "绑定成功，即将刷新",
					confirmButtonText: "完成"
				});
				location.reload()
			}
		})
		// alert("数据: " + data + "\n状态: " + status);
	}
</script>
